/*
 * @Description:
 * @Version: 2.0
 * @Autor: Seven
 * @Date: 2023-10-19 16:08:00
 * @LastEditors: Seven
 * @LastEditTime: 2023-10-26 15:14:41
 */
import { getYearAdd, getYearEdit } from "@/services/modules/plan";
import { loacalCache } from "@/ultils/func";
import { Form, Input, Modal, Select, message } from "antd";
import React, { memo, useEffect, useRef } from "react";
const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 6 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 18 },
  },
};
const { Option } = Select;
const AddModal = memo(
  ({ modalVisible, handleCancel, getList, title = "新增", rowInfoData }) => {
    const formRef = useRef();
    const [form] = Form.useForm();
    function onFinish() {
      form
        .validateFields({ validateOnly: true })
        .then(() => {
          let data = form.getFieldsValue();
          if (title === "编辑") {
            getYearEdit(rowInfoData.id, data).then((res) => {
              if (res.code === 200) {
                getList();
                handleCancel();
              } else {
                message.warning(res?.msg);
              }
            });
          } else {
            getYearAdd(data).then((res) => {
              if (res.code === 200) {
                getList();
                handleCancel();
              } else {
                message.warning(res?.msg);
              }
            });
          }
        })
        .catch((err) => err);
    }

    useEffect(() => {
      if (title === "编辑") {
        form.setFieldsValue(rowInfoData);
      }
    }, [rowInfoData, form, title]);

    return (
      <div>
        <Modal
          title={title}
          open={modalVisible}
          onOk={onFinish}
          onCancel={handleCancel}
        >
          <Form
            {...formItemLayout}
            ref={formRef}
            onFinish={onFinish}
            form={form}
            style={{
              maxWidth: 600,
            }}
          >
            <Form.Item
              name="planId"
              label="年计划"
              rules={[
                {
                  required: true,
                  message: "请输入你的年计划id",
                },
              ]}
            >
              <Input />
            </Form.Item>
            <Form.Item
              name="planDesc"
              label="年计划描述"
              rules={[
                {
                  required: true,
                  message: "请输入你的年计划描述",
                },
              ]}
            >
              <Input />
            </Form.Item>
            <Form.Item
              name="limitYear"
              label="年限"
              rules={[
                {
                  required: true,
                  message: "请输入你的年限",
                },
              ]}
            >
              <Input />
            </Form.Item>
            <Form.Item
              name="dept"
              label="部门"
              rules={[
                {
                  required: true,
                  message: "请输入你的部门",
                },
              ]}
            >
              <Input />
            </Form.Item>
            <Form.Item
              name="status"
              label="状态"
              rules={[
                {
                  required: true,
                  message: "请输入你的状态",
                },
              ]}
            >
              <Select>
                {loacalCache.getCache("DicDataList")["plan_status"] &&
                  loacalCache
                    .getCache("DicDataList")
                    ["plan_status"].map((item) => {
                      return (
                        <Option value={item.value} key={item.id}>
                          {item.label}
                        </Option>
                      );
                    })}
              </Select>
            </Form.Item>
            <Form.Item
              name="workshop"
              label="车间"
              rules={[
                {
                  required: true,
                  message: "请输入你的车间",
                },
              ]}
            >
              <Select>
                {loacalCache.getCache("DicDataList")["workshop"] &&
                  loacalCache
                    .getCache("DicDataList")
                    ["workshop"].map((item) => {
                      return (
                        <Option value={item.value} key={item.id}>
                          {item.label}
                        </Option>
                      );
                    })}
              </Select>
            </Form.Item>
          </Form>
        </Modal>
      </div>
    );
  }
);

export default AddModal;
